package com.ss.android.common.util;

import android.net.Uri;
import android.text.TextUtils;

import com.bytedance.common.utility.b.ThreadPlus;
import com.bytedance.common.utility.Logger;

import org.json.JSONObject;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class EventSender implements Runnable {
    public String mHost = null;
    public boolean mSenderEnable = false;
    private BlockingQueue<JSONObject> mEventQueue = new LinkedBlockingQueue();
    private ThreadPlus mThreadPlus;
    private static EventSender sEventsSender;

    private EventSender() {
    }

    public static EventSender inst() {
        if (EventSender.sEventsSender == null) {
            Class v1 = EventSender.class;
            synchronized (v1) {
                if (EventSender.sEventsSender == null) {
                    EventSender.sEventsSender = new EventSender();
                }
            }
        }

        return EventSender.sEventsSender;
    }

    public final void setSenderEnable(boolean arg5) {
        synchronized (this) {
            if (this.mSenderEnable != arg5) {
                this.mSenderEnable = arg5;
                if ((this.mSenderEnable) && EventSender.sEventsSender != null) {
                    this.mThreadPlus = new ThreadPlus(EventSender.sEventsSender, "EventSender", true);
                    this.mThreadPlus.start();
                    return;
                }
                this.mThreadPlus = null;
            }
            return;
        }
    }

    public final void putEvent(JSONObject arg2) {
        if (this.mSenderEnable) {
            this.mEventQueue.add(arg2);
        }
    }

    public void run() {
        Object v0_1;
        try {
            while (true) {
                label_0:
                if (Thread.interrupted()) {
                    return;
                }

                if (!this.mSenderEnable) {
                    return;
                }

                if (TextUtils.isEmpty(this.mHost)) {
                    return;
                }

                v0_1 = this.mEventQueue.take();
                if (v0_1 == null) {
                    continue;
                }
                try {
                    Uri.Builder v1_1 = Uri.parse("http://" + this.mHost + "/").buildUpon();
                    v1_1.appendQueryParameter("parameter", v0_1.toString());
                    String v1_2 = com.bytedance.common.utility.e.a().a(v1_1.toString());
                    if ("success".equals(new JSONObject(v1_2).opt("data"))) {
                        Logger.d("EventSender", "send success event = " + ((JSONObject) v0_1).toString() + " resJson = " + v1_2);
                        continue;
                    }

                    Logger.d("EventSender", "send fail event = " + ((JSONObject) v0_1).toString() + " resJson = " + v1_2);
                    continue;
                } catch (Exception v1) {
                    Logger.d("EventSender", "send exception event = " + ((JSONObject) v0_1).toString() + " e = " + v1.getMessage());
                    continue;
                }
            }
        } catch (Exception v0) {
            return;
        }


    }
}

